<?php
/**
 * 省市区模型
 */

namespace app\badmin\model;


use app\admin\model\Region as RegionModel;
use app\common\model\BaseModel;
use custom\Err;
use PDOStatement;
use think\Collection;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;

class Region extends BaseModel {

    /**
     * city_code查询
     * @param $city_code
     * @return mixed
     */
    public static function cityCodeTodata($city_codes) {
        foreach ($city_codes as $k => $v) {
            if (!$v)
                unset($city_codes[$k]);
        }
        $city_codes = array_merge($city_codes);
        foreach ($city_codes as $k1 => $v1) {
            $data[] = self::getOne([['city_code', '=', $v1]], 'name')['name'];
        }
        return $data;
    }

    /**
     * 筛选多条
     * @param $map
     * @param $column
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getDatas($map, $column) {
        return db('region')
            ->where($map)
            ->field($column)
            ->select();
    }

    /**
     * 筛选单条
     * @param $map
     * @param $column
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getOne($map, $column) {
        return db('region')
            ->where($map)
            ->field($column)
            ->find();
    }

    /**
     * 首页定位省市结构
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function indexCity($params) {
        $company = Company::getOne([['id', '=', $params['company_id']]], 'city_code');
        $city_code = explode(',', $company['city_code']);
        $arr = [];
        foreach ($city_code as $k => $v) {
            $vl = explode('-', $v);
            $arr[$k]['key'] = $vl[0];
            $arr[$k]['val'] = $vl[1];
        }
        return $arr;
//        $company = Company::getOne([['id', '=', $params['company_id']]], 'city_code');
//        $city_code = explode(',', $company['city_code']);
//        foreach ($city_code as $k => $v) {
//            $city_code_ = explode('-', $v);
//            $city_code[$k] = $city_code_[0];
//        }
//        $p_code = self::where("city_code", "in", $city_code)->field('p_code')->select()->toArray();//省code
//        $p_code = array_column($p_code, 'p_code');
//        $p_code = array_flip(array_flip($p_code));//去重
//        $city_code = array_merge($p_code, $city_code);//合并省code和城市code
//        $map[] = ['city_code', "in", $city_code];
//        $map[] = ['rank', 'in', '1,2'];//省市
//        $map[] = ['id', 'not in', [25179, 25183]];
//        $column = ['name', 'city_code', 'id', 'pid', 'initials'];
//        $data = db('region')
//            ->where($map)
//            ->field($column)
//            ->order(['rank' => 'desc', 'initials' => 'asc'])
//            ->select();
//        return self::treeFilter($data);
    }

    /**
     * 获取树
     * @param $data
     * @return array
     */
    public static function treeFilter($data) {
        $data = array_column($data, null, 'id');
        $tree = [];
        foreach ($data as &$val) {
            if (isset($data[$val['pid']])) {//市
                $data[$val['pid']]['children'][] = [
                    'value'    => $val['city_code'],
                    'label'    => $val['name'],
                    'children' => $val['children'] ?? [],
                ];
            } else {//省
                $arr = [
                    'value'    => $val['city_code'],
                    'label'    => $val['name'],
                    'children' => $val['children'] ?? [],
                ];
                $tree[$val['initials']]['label'] = $val['initials'];
                $tree[$val['initials']]['children'][] = $arr;
            }
        }
        return array_values($tree);
    }

}
